<?php defined('SYSPATH') OR die('No direct access allowed.');

class WB_FormItem extends WB_Form {
	
	protected $name = '';
	protected $params = array();
	protected $value = null;
	protected $db_table = '';			//table the item "belongs to"
	protected $table_columns = null;	//columns needed for table view
	protected $form_columns = null;		//columns needed for form view
	protected $db_columns = null;		//columns needed for adding or editing item
	protected $table_joins = null;
	protected $form_joins = null;
	protected $db_joins = null;
	protected $label = '';
	protected $prefix = '<p>';
	protected $suffix = "</p>\n\n";
	protected $attributes = array();
	protected $table_class = '';
	protected $description = '';
	protected $error_msg = null;		//reserverved for errors which stop the user from sending the form
	
	public function __construct($name='', $params=array(), $prefix_class='text')
	{
		parent::__construct();
		
		$this->name = $name;
		if ($prefix_class) $this->prefix = '<p class="'.$prefix_class.'">';
		foreach ($params as $k=>$v) $this->$k = $v;
		
		$this->form_columns[$this->name] = $this->db_table.'.'.$this->name;
		$this->table_columns[$this->name] = $this->db_table.'.'.$this->name;
		$this->db_columns[$this->name] = $this->db_table.'.'.$this->name;
	}
	
	/**
	 * Prepare a value for database insertation
	 * 
	 * @param mixed $value
	 * @return mixed
	 */
	public function prep_db()
	{
		if ( ! empty($this->db_columns)) return $this->value;
		return null;
	}
	
	/**
	 * Returns a name for the "form" view
	 * 
	 * @return string
	 */
	public function get_label()
	{
		return empty($this->label) ? utf8::ucfirst($this->name) : $this->label;
	}
	
	/**
	 * Returns a name for the "table" view
	 * 
	 * @return string
	 */
	public function get_display_name()
	{
		return 
			isset($this->params['display_name']) ? 
				$this->params['display_name'] :
				$this->get_label();
	}
	
	/**
	 * Sets the value for the class
	 * 
	 * @param mixed $value
	 * @param string type table|form|edit|add
	 * @return void
	 */
	public function set_value($value, $type='')
	{
		$this->value = $value;
	}
	
	/**
	 * Returns html value for a table cell
	 * 
	 * @return string
	 */
	public function render_cell()
	{
		$value = is_array($this->value) ? $this->value[$this->name] : $this->value;
		return empty($value) && ! is_numeric($value) ? '&nbsp;' : (string)$value;
	}

	/**
	 * Renders the form element
	 * 
	 * @return string
	 */
	public function render_item()
	{
		$str = $this->prefix;
		$str .= form::label($this->name, $this->label);
		$str .= form::input($this->name, $this->value, html::attributes($this->attributes));
		if ($this->description) $str .= '<span class="description">'.$this->description.'</span>';
		$str .= $this->suffix;
		
		return $str;
	}
	
	/**
	 * Returns the class of the object
	 * Needed function for checking if file types are present
	 * @return string
	 */
	public function get_class()
	{
		return get_class($this);
	}
	
	/**
	 * Returns all database names related to the object
	 * Needed function for escaping my where statements
	 * @return array
	 */
	public function get_escape()
	{
		$ret_val = array($this->db_table);
		if (is_array($this->form_columns)) foreach ($this->form_columns as $k=>$v) {$ret_val[] = $k; $ret_val[] = explode('.',$v);}
		if (is_array($this->table_columns)) foreach ($this->table_columns as $k=>$v) {$ret_val[] = $k; $ret_val[] = explode('.',$v);}
		if (is_array($this->form_joins)) foreach ($this->form_joins as $k=>$v) {$ret_val[] = $k; $ret_val[] = explode('.',$v);}
		if (is_array($this->table_joins)) foreach ($this->table_joins as $k=>$v) {$ret_val[] = $k; $ret_val[] = $v;}
		return $ret_val;
	}
}

//End of WB_FormItem Library